package com.bzm.common.rabbitmq;

import java.io.IOException;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.bzm.SpringBeanUtil;
import com.bzm.config.BzmGlobalConfig;
import com.bzm.pojo.plugins.im.ImChatMsg;
import com.bzm.service.plugins.BzmImCommonService;
import com.rabbitmq.client.Channel;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class FanoutReceiver {
	
	@RabbitListener(queues = BzmGlobalConfig.im_msg_fanout_queue_mq)
	@RabbitHandler
    public void imMsgFanoutQueueConsumer(Message message,Channel channel) {
		byte[] msgBody = message.getBody();
		if (msgBody != null && msgBody.length > 0) { 
			//打印数据 String message	
			String msgObj = new String(msgBody);
			//log.info(BzmGlobalConfig.im_msg_fanout_queue_mq+" 开始消费--->   msg ： "+ msgObj);	
				
			ImChatMsg imChatMsg = JSON.parseObject(msgObj, ImChatMsg.class);
			if(imChatMsg!=null) {
				BzmImCommonService bzmImCommonService = (BzmImCommonService)SpringBeanUtil.getBean("bzmImCommonServiceImpl");
				bzmImCommonService.consumeMsg(imChatMsg);
			}
			try {		
				//告诉服务器收到这条消息 已经被我消费了 可以在队列删掉 这样以后就不会再发了 否则消息服务器以为这条消息没处理掉 后续还会在发
				channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
    }

}
